package flc.ast.fragment;

import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;

import android.os.StatFs;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.blankj.utilcode.util.ConvertUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;

import java.util.ArrayList;
import java.util.List;

import flc.ast.R;
import flc.ast.activity.ApkCollActivity;
import flc.ast.activity.ApkDetailsActivity;
import flc.ast.activity.ApkExportActivity;
import flc.ast.activity.FileClearActivity;
import flc.ast.activity.RecentUninstallActivity;
import flc.ast.activity.SearchApkActivity;
import flc.ast.adapter.HomeUninstallAdapter;
import flc.ast.adapter.SearchApkAdapter;
import flc.ast.bean.ApkBean;
import flc.ast.databinding.FragmentHomeBinding;
import flc.ast.utils.AppSpUtils;
import stark.common.basic.base.BaseNoModelFragment;
import stark.common.basic.device.StorageUtil;
import stark.common.basic.event.EventStatProxy;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link HomeFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class HomeFragment extends BaseNoModelFragment<FragmentHomeBinding> {

    private HomeUninstallAdapter uninstallAdapter;
    private List<ApkBean> listShow = new ArrayList<>();

    @Override
    protected int onCreate() {
        return R.layout.fragment_home;
    }

    @Override
    protected void initView() {
        EventStatProxy.getInstance().statEvent1(getActivity(), mDataBinding.event1Container);
        EventStatProxy.getInstance().statEvent5(getActivity(), mDataBinding.event5Container);

        mDataBinding.tvSearch.setOnClickListener(this);
        mDataBinding.ivApkCollect.setOnClickListener(this);
        mDataBinding.ivExportRecord.setOnClickListener(this);
        mDataBinding.ivFileClear.setOnClickListener(this);
        mDataBinding.rlRecentUninstall.setOnClickListener(this);

        mDataBinding.rvUninstallList.setLayoutManager(new LinearLayoutManager(mContext));
        uninstallAdapter = new HomeUninstallAdapter();
        mDataBinding.rvUninstallList.setAdapter(uninstallAdapter);
        uninstallAdapter.setOnItemClickListener(this);
    }

    @Override
    protected void onItemClickCallback(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        ApkBean item = (ApkBean) adapter.getItem(position);
        ApkDetailsActivity.apkBean = item;
        startActivity(ApkDetailsActivity.class);
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()) {
            case R.id.tvSearch://搜索
                String text = mDataBinding.etSearch.getText().toString();
                SearchApkActivity.searchText = text;
                startActivity(SearchApkActivity.class);
                break;
            case R.id.ivApkCollect://收藏APK文件
                startActivity(ApkCollActivity.class);
                break;
            case R.id.ivExportRecord://APK导出记录
                startActivity(ApkExportActivity.class);
                break;
            case R.id.ivFileClear://文件清理
                startActivity(FileClearActivity.class);
                break;
            case R.id.rlRecentUninstall://最近卸载
                startActivity(RecentUninstallActivity.class);
                break;
        }
    }

    @Override
    protected void initData() {
        StatFs statFs = StorageUtil.getInternalMemory();//内部存储
        //已使用
        long usedLong = statFs.getTotalBytes() - statFs.getAvailableBytes();
        //总内存
        long allLong = statFs.getTotalBytes();
        //已用百分比
        int progress = Math.round((statFs.getTotalBytes() - statFs.getAvailableBytes()) * 100f / statFs.getTotalBytes());
        mDataBinding.tvStorageNum.setText(getString(R.string.storage_num, ConvertUtils.byte2FitMemorySize(allLong, 0), ConvertUtils.byte2FitMemorySize(usedLong, 0)));
    }

    @Override
    public void onResume() {
        super.onResume();
        getData();
    }

    //获取卸载记录数据
    private void getData() {
        listShow.clear();
        List<ApkBean> listSp = AppSpUtils.getUninstallRecordList();
        if (listSp != null && listSp.size() > 0) {
            listShow.addAll(listSp);
            uninstallAdapter.setList(listShow);
            mDataBinding.rvUninstallList.setVisibility(View.VISIBLE);
        } else {
            mDataBinding.rvUninstallList.setVisibility(View.GONE);
        }
    }

}